package com.objectdraw.client;

import java.awt.*;
import java.awt.event.*;
import java.net.SocketException;

import com.objectdraw.client.objectdraw.*;
/**
 * Takes incoming messages from the server and processes them. Prints the
 * message to the console and acts upon the commands.
 * 
 * @author jgulik, cfruehan
 *
 */
public class EchoThread extends Thread {
	clientcomm com;
	DrawingCanvas canvas;

	/**
	 * Instantiates an echo thread.
	 * 
	 * @param com recieves messages from this clientcomm object
	 * @param canvas 
	 */
	public EchoThread(clientcomm com, DrawingCanvas canvas) {
		this.com = com;
		this.canvas = canvas;
	}

	/**
	 * As long as the clientcomm object is available messages from the inputstream are read
	 * and handled.  The message is printed to the console and actions are taken based upon 
	 * the command.
	 */
	public void run() {
		while (com != null) {
			try {
				String message = com.readMessage();
				System.out.println("recieved:" + message);
				String[] params = message.split(" ");
				String command = params[0];
				// for(int i=0;i<params.length;i++){
				// System.out.println(params[i]);
				// }

				// Control command
				if (command.equalsIgnoreCase("Control")) {
					// do control stuff

					if (params[1].equalsIgnoreCase("canvas")
							&& params.length < 3) {
						canvas.control = true;
						// System.out.println("doing:canvas.control=true;");
					} else if (params[2].equalsIgnoreCase("denied")) {
						canvas.control = false;
						// System.out.println("doing:canvas.control=false;");
					}

				}
				// tool command
				else if (command.equalsIgnoreCase("tool")) {
					canvas.setCurrentTool(params[1]);
				}
				// clear command
				else if (command.equalsIgnoreCase("clear")) {
					canvas.clearCanvas();
				}
				// delete command
				else if (command.equalsIgnoreCase("delete")) {
					if (canvas.magicBox.active) {
						if (canvas.firstObject.next != null) {
							canvas.firstObject.next.prev = null;
							canvas.firstObject = canvas.firstObject.next;
						} else {
							canvas.firstObject = null;
							canvas.lastObject = null;
						}

						canvas.magicBox.active = false;
						canvas.repaint();
					}
				}
				// color command
				else if (command.equalsIgnoreCase("color")) {
					if (params[1].equalsIgnoreCase("red")) {
						canvas.setPenColor(Color.RED);
					} else if (params[1].equalsIgnoreCase("blue")) {
						canvas.setPenColor(Color.BLUE);
					} else if (params[1].equalsIgnoreCase("green")) {
						canvas.setPenColor(Color.GREEN);
					} else {
						canvas.setPenColor(Color.BLACK);
					}
				}
				// mouse command
				else if (command.equalsIgnoreCase("mouse")) {
					if (params[1].equalsIgnoreCase("dragged")) {
						canvas.currentTool.mouseDragged(new MouseEvent(canvas,
								0, 0, 0, Integer.parseInt(params[2]), Integer
										.parseInt(params[3]), 0, false));
					} else if (params[1].equalsIgnoreCase("released")) {
						canvas.currentTool.mouseReleased(new MouseEvent(canvas,
								0, 0, 0, Integer.parseInt(params[2]), Integer
										.parseInt(params[3]), 0, false));
					} else if (params[1].equalsIgnoreCase("pressed")) {
						canvas.currentTool.mousePressed(new MouseEvent(canvas,
								0, 0, 0, Integer.parseInt(params[2]), Integer
										.parseInt(params[3]), 0, false));
					}
				} else if (command.equalsIgnoreCase("key")) {
					((KeyboardTool) canvas.currentTool)
							.keyPressed(new KeyEvent(canvas, 0, 0, 0, 0,
									params[1].charAt(0), 0));

				} else if (command.equalsIgnoreCase("fullupdate")) {
					int i = 1;

					canvas.clearCanvas();

					while (!params[i].equalsIgnoreCase("end")) {
						if (params[i].equalsIgnoreCase("line")) {

							String color = params[i+1];
							String x1 = params[i+2];
							String y1 = params[i+3];
							String x2 = params[i+4];
							String y2 = params[i+5];
							canvas.push(new IDrawingObjectLink(
									new LineObject(x1,y1, x2,y2,color)));
							i = i + 6;

						} else if (params[i].equalsIgnoreCase("rectangle")) {
							canvas.push(new IDrawingObjectLink(
									new RectangleObject(params[i + 2],
											params[i + 3], params[i + 4],
											params[i + 5], params[i + 1])));
							i = i + 6;

						} else if (params[i].equalsIgnoreCase("oval")) {
							canvas.push(new IDrawingObjectLink(
									new OvalObject(params[i + 2],
											params[i + 3], params[i + 4],
											params[i + 5], params[i + 1])));
							i = i + 6;

						} else if (params[i].equalsIgnoreCase("key")) {
							canvas.push(new IDrawingObjectLink(new TextObject(
									params[i + 2], params[i + 3],
									params[i + 1], params[i + 4])));
							i = i + 5;

						} else {
							FreehandDrawingObject fdo = new FreehandDrawingObject(
									params[i + 2], params[i + 3], params[i + 1]);
							i = i + 3;

							while (params[i + 1].matches("[0-9]+")) {
								fdo.add(new Point(Integer
										.parseInt(params[i + 1]), Integer
										.parseInt(params[i + 2])));
								i = i + 2;

							}

							canvas.push(new IDrawingObjectLink(fdo));

							i++;
						}// end else

					}// end while

					canvas.repaint();
				}// end fullupdate command section

			} catch (SocketException e) {
				System.err.println("server or client died or something");
				System.exit(0);
			} catch (Exception e) {
				e.printStackTrace();
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
			}
		}
	}
}
